package honeypot.yhgl.action;

import honeypot.yhgl.pojo.User;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.general.DefaultPieDataset;

import cm.util.hibernate.HibernateSessionFactory;
import com.opensymphony.xwork2.ActionSupport;
public class SearchAction extends ActionSupport {
	 private double admin_num;
	 private double user_num;
	 private String hql_admin;
	 private Session session_admin;
	 private Transaction trans_admin;
	 private Query query_admin;
	 private String hql_user;
	 private Session session_user;
	 private Transaction trans_user;
	 private Query query_user;
	 public double getAdmin_num() {
		return admin_num;
	}
	public void setAdmin_num(double adminNum) {
		admin_num = adminNum;
	}
	public double getUser_num() {
		return user_num;
	}
	public void setUser_num(double userNum) {
		user_num = userNum;
	}
	public String getHql() {
		return hql_admin;
	}
	public void setHql(String hql) {
		this.hql_admin = hql;
	}
	public Session getSession() {
		return session_admin;
	}
	public void setSession(Session session) {
		this.session_admin = session;
	}
	public Transaction getTrans() {
		return trans_admin;
	}
	public void setTrans(Transaction trans) {
		this.trans_admin = trans;
	}
	
	public String getHql_admin() {
		return hql_admin;
	}
	public void setHql_admin(String hqlAdmin) {
		hql_admin = hqlAdmin;
	}
	public Session getSession_admin() {
		return session_admin;
	}
	public void setSession_admin(Session sessionAdmin) {
		session_admin = sessionAdmin;
	}
	public Transaction getTrans_admin() {
		return trans_admin;
	}
	public void setTrans_admin(Transaction transAdmin) {
		trans_admin = transAdmin;
	}
	public Query getQuery_admin() {
		return query_admin;
	}
	public void setQuery_admin(Query queryAdmin) {
		query_admin = queryAdmin;
	}
	public String getHql_user() {
		return hql_user;
	}
	public void setHql_user(String hqlUser) {
		hql_user = hqlUser;
	}
	public Session getSession_user() {
		return session_user;
	}
	public void setSession_user(Session sessionUser) {
		session_user = sessionUser;
	}
	public Transaction getTrans_user() {
		return trans_user;
	}
	public void setTrans_user(Transaction transUser) {
		trans_user = transUser;
	}
	public Query getQuery_user() {
		return query_user;
	}
	public void setQuery_user(Query queryUser) {
		query_user = queryUser;
	}
	public String execute() throws Exception
	{
	  session_admin=HibernateSessionFactory.getSession();
	  trans_admin=session_admin.beginTransaction();
	  session_user=HibernateSessionFactory.getSession();
	  trans_user=session_user.beginTransaction();
	  /*以上为HQL语句创建Query对象，执行setString方法为HQL语句的参数赋值，
	   * 
	   */
//查		Query query=session.createQuery("from User as u where u.type=:t");
//寻		query.setString("t", "admin");//设置查询参数
//	     List l=query.list();	  
//权		System.out.println("用户名"+"      "+"权限");
	//遍历查询结果
//		for(Iterator sit=l.iterator();sit.hasNext();)
//为		{
//			User us=(User)sit.next();
//的			System.out.println(us.getUsername()+"      "+us.getType());
//项		}
	    hql_admin="select count(*) from User where type=:t";
	    query_admin=session_admin.createQuery(hql_admin);
		query_admin.setString("t", "admin");//设置查询参数
		hql_user="select count(*) from User where type=:t";
	    query_user=session_user.createQuery(hql_user);
		query_user.setString("t", "user");//设置查询参数
		admin_num=Double.parseDouble(query_admin.iterate().next().toString());   
		user_num=Double.parseDouble(query_user.iterate().next().toString());    
		System.out.println("类型为"+"admin"+"的用户数量为"+(admin_num));
		System.out.println("类型为"+"user"+"的用户数量为"+(user_num));
       //下面为制作饼图的代码
    	
		DefaultPieDataset dataset= new DefaultPieDataset();
        dataset.setValue("管理员", admin_num);
		dataset.setValue("用户", user_num);
  JFreeChart chart=ChartFactory.createPieChart("注册用户饼图", dataset, true, false, false);
        chart.getTitle().setFont(new Font("宋体",Font.BOLD,15));
		PiePlot plot=(PiePlot)chart.getPlot();
		plot.setLabelGenerator(new StandardPieSectionLabelGenerator( 
				"{0}={1}({2})", NumberFormat.getNumberInstance(), 
				new DecimalFormat("0.00%"))); 
		plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( 
		"{0}={1}({2})",NumberFormat.getNumberInstance(), 
		new DecimalFormat("0.00%"))); 
		plot.setLabelFont(new Font("宋体",Font.BOLD,10));
		plot.setBackgroundPaint(Color.WHITE);
		plot.setBackgroundImageAlpha(0.65f);
		plot.setForegroundAlpha(0.65f); 
		plot.setCircular(false, false); 

		LegendTitle legend=chart.getLegend();
		legend.setItemFont(new Font("宋体",Font.BOLD,10));
		
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart,
					800, 500);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
       
		//提交事务
		session_admin.close();
		//session_user.close();
		System.out.println("成功");
	
		return null;
	}
}
